package com.xpgk.common.kit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
 
public class NetKit {
	/** 
     * 获取操作系统名称 
     */  
    public static String getOsName() {  
        String os = "";  
        os = System.getProperty("os.name");  
        return os;  
    }  
	 /** 
     * 获取MAC地址 
     */  
    public static String getMACAddress() {  
        String address = "";  
        String os = getOsName();  
        //根据操作系统类型获取MAC地址  
        if (os.startsWith("Windows")) {  
            try {  
                String command = "cmd.exe /c ipconfig /all";  
                Process p = Runtime.getRuntime().exec(command);  
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GB2312"));  
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.indexOf("Physical Address") > 0 || line.indexOf("物理地址") > 0) {  
                        int index = line.indexOf(":");  
                        index += 2;  
                        address = line.substring(index); 
                        address = address.replace("-", "");
                        break;  
                    }  
                }  
                br.close();  
                return address.trim();  
            } catch (IOException e) {  
            }  
        } else if (os.startsWith("Linux")) {  
            String command = "/bin/sh -c ifconfig -a";  
            Process p;  
            try {  
                p = Runtime.getRuntime().exec(command);  
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line;  
                while ((line = br.readLine()) != null) {  
                    if (line.indexOf("HWaddr") > 0) {  
                        int index = line.indexOf("HWaddr") + "HWaddr".length();  
                        address = line.substring(index);  
                        address = address.replace("-", "");
                        break;  
                    } 
                    else if (line.indexOf("物理地址")>0) {  
                        int index = line.indexOf("物理地址") + "物理地址".length();  
                        address = line.substring(index);  
                        address = address.replace("-", "");
                        break;  
                    }
                }  
                br.close();  
            } catch (IOException e) {  
            }  
        }  
        address = address.trim();  
        return address;  
    }  
    /** 
     * 根据IP地址获取MAC地址 
     */  
    public static String getMACAddress(String ipAddress) {  
        String str = "", strMAC = "", macAddress = "";  
        try {  
            Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);  
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());  
            LineNumberReader input = new LineNumberReader(ir);  
            for (int i = 1; i < 100; i++) {  
                str = input.readLine();  
                if (str != null) {  
                    if (str.indexOf("MAC Address") > 1) {  
                        strMAC = str.substring(str.indexOf("MAC Address") + 14, str.length());  
                        break;  
                    }  
                }  
            }  
        } catch (IOException ex) {  
            return "Can't Get MAC Address!";  
        }  
        if (strMAC.length() < 17) {  
            return "Error!";  
        }  
        macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) + ":"  
                + strMAC.substring(12, 14) + ":" + strMAC.substring(15, 17);  
        return macAddress;  
    }  
	public static String getHostIp(){  
		InetAddress addr = null;
	    try {
	        addr = InetAddress.getLocalHost();
	    } catch (UnknownHostException e) {
	         e.printStackTrace();
	    }
	    if(null == addr){  
	        return null;  
	    }  
	    return addr.getHostAddress(); //get the ip address  
	}

    public static String getClientIP(HttpServletRequest request) {
	     String ip = request.getHeader("X-Forwarded-For");
	     if(!PubKit.isBlank(ip) && !"unKnown".equalsIgnoreCase(ip)){
	         //多次反向代理后会有多个ip值，第一个ip才是真实ip
	         int index = ip.indexOf(",");
	         if(index != -1){
	             return ip.substring(0,index);
	         }else{
	             return ip;
	        }
	    }
	    ip = request.getHeader("X-Real-IP");
	    if(!PubKit.isBlank(ip) && !"unKnown".equalsIgnoreCase(ip)){
	        return ip;
	    }
	    return request.getRemoteAddr();
	}
    
    /** 
     * 判断字符串的编码 
     * 
     * @param str 
     * @return 
     */  
    public static String getEncoding(String str) {  
        String encode[] = new String[]{  
                "UTF-8",  
                "ISO-8859-1",  
                "GB2312",  
                "GBK",  
                "GB18030",  
                "Big5",  
                "Unicode",  
                "ASCII"  
        };  
        for (int i = 0; i < encode.length; i++){  
            try {  
                if (str.equals(new String(str.getBytes(encode[i]), encode[i]))) {  
                    return encode[i];  
                }  
            } catch (Exception ex) {  
            }  
        }  
          
        return "";  
    }  
}